﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Common;
using Think.Lenovo.Com.Models;
namespace Think.Lenovo.Com.DataAccess
{
    /// <summary>
    /// 角色和角色级别的关系
    /// </summary>
    public class RoleUserLevelRelation
    {
        /// <summary>
        /// 增加关系
        /// </summary>
        /// <returns></returns>
        public long Add(int roleId, int userLevelId)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append(" Insert Into  tbl_role_userlevel_r (roleId, userLevelId) ")
                .Append(" values (@roleId,@userLevelId)");
            MySqlParameter[] ps = { 
                                    new MySqlParameter("@roleId",MySqlDbType.Int32),
                                    new MySqlParameter("@userLevelId",MySqlDbType.Int32)
                                  };
            ps[0].Value = roleId;
            ps[1].Value = userLevelId;
            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;

        }
        /// <summary>
        /// 删除关系
        /// </summary>
        /// <returns></returns>
        public int Delete(int roleId)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append(" Delete From  tbl_role_userlevel_r  ")
                .Append(" Where roleId=@roleId");
            MySqlParameter[] ps = { 
                                    new MySqlParameter("@roleId",MySqlDbType.Int32)
                                  };
            ps[0].Value = roleId;
            int affect = MysqlHelper.Delete(conn, sql.ToString(), ps);
            return affect;

        }
        /// <summary>
        /// 根据一个角色id 得到该角色的所有级别Id
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public LinkedList<RoleUserLevelRelationModel> GetLeveles(int roleId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_role_userlevel_r where roleId=" + roleId);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            LinkedList<RoleUserLevelRelationModel> list = new LinkedList<RoleUserLevelRelationModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        RoleUserLevelRelationModel model = new RoleUserLevelRelationModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.RoleId = Convert.ToInt32(row["roleId"]);
                        model.UserLevelId = Convert.ToInt32(row["userLevelId"]);
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }
    }
}
